import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const userenwuStore = defineStore('renwu', () => {
  //任务列表
  var shuju = ref([])
  //未完成
  var weiwancheng = computed(() => {
    return shuju.value.filter((item) => !item.zhuangtai).length;
  })

  //新增任务
  var xinzeng = (payload) => {
    shuju.value.push({
      id: shuju.value.length + 1, name: payload, zhuangtai: false
    })
  }
  //更改任务
  var genggai = (payload) => {
    var task = shuju.value.find((item) => {
      return item.id = payload
    })
    if (task) {
      task.zhuangtai = !task.zhuangtai
    }
  }
  //删除任务
  var del = (payload) => {
    var index = shuju.value.findIndex((item) => {
      return item.id = payload
    })
    if (index != -1) {
      shuju.value.splice(index, 1)
    }
  }

  return { shuju, weiwancheng,xinzeng, genggai, del }
})
